<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Member_Car extends MY_Controller {
	
	//User should not come here.  This controller only used for adding/editing member car photographs
	function index() {
		define("CAR_SEGMENT", 3);
		define("NOT_REQUIRED_TO_LOGIN", FALSE);
        
        $basic_data['first_name'] = $this->first_name;
        $basic_data['additional_javascript_references'] = '<script type="text/javascript" src="'.base_url().'js/member_car_form_functions.js"></script>
                                                           <script type="text/javascript" src="'.base_url().'js/jquery.colorbox-min.js"></script>
                                                          ';
        $basic_data['additional_css_references'] = link_tag('css/colorbox.css'.'?'.time());
		
		
		$car_id = $this->uri->segment(CAR_SEGMENT);

		if (!$car_id)
			redirect('member_profile');
		
        $basic_data['additional_javascript_doc_ready_functions'] = "additional_doc_ready_code();";
		
        $form_basic_info    =   $this->_set_form_basics(NOT_REQUIRED_TO_LOGIN);
        $car_info           =   $this->_get_car_information($car_id);
		$car_gallery        =   $this->_get_car_photos($car_id, true);
		
		
        $data = array_merge($form_basic_info, $basic_data, $car_info, $car_gallery);
		
		$data['return_location_html'] = 'member_profile/index/'.$car_info['member_id'];
		$data['return_location'] = 'member profile';
        $data['title'] = 'Central Alabama VW Club -- View Car';
        $data['heading'] = 'Car Info';
        
        $data['view'] = 'car_profile_view';
        $this->load->vars($data);        
        $this->load->view('master_view', $data);
        return;

	}
    function delete_image() {
        define("ID_SEGMENT", 3);
        $this->load->helper('file');
        
        $photo_id = $this->uri->segment(ID_SEGMENT);
        $this->load->model('Car_model');
        $row = $this->Car_model->Get_car_image($photo_id);
        
        $pic_name = $row->pic_name;
        
        unlink($_SERVER['DOCUMENT_ROOT'].'/member_car_gallery/'.$pic_name.'.jpg');
        unlink($_SERVER['DOCUMENT_ROOT'].'/member_car_gallery/thumbnail/'.$pic_name.'_thumb.jpg');
        $this->Car_model->Delete_car_image($photo_id);
        
        redirect(base_url().'index.php?/member_car/edit_car/'.$row->car_id);
        return;
    }
    function make_profile_photo() {
        define("ID_SEGMENT", 3);
        $this->load->helper('file');
        
        $photo_id = $this->uri->segment(ID_SEGMENT);
        $this->load->model('Car_model');
        $car_id = $this->Car_model->Make_profile_photo($photo_id);

        redirect(base_url().'index.php?/member_car/edit_car/'.$car_id);
        return;
    }
    function new_car() {
        $this->load->model('Car_model');
        $data = $this->_get_form_post_data();
		$new_id = $this->Car_model->Add_car($data);
		redirect(base_url().'index.php?/member_car/edit_car/'.$new_id);
    }
	function add_car() {
        
        $this->load->model('Lookup_model');
        
        $basic_data['first_name'] = $this->first_name;
        $basic_data['save_type'] = 'new_car';
		$basic_data['error'] = '';
        $basic_data['additional_javascript_references'] = '<script type="text/javascript" src="'.base_url().'js/member_car_form_functions.js"></script>
                                                           <script type="text/javascript" src="'.base_url().'js/jquery.colorbox-min.js"></script>
                                                          ';
        $basic_data['additional_css_references'] = link_tag('css/colorbox.css'.'?'.time());
        
        
        $basic_data['additional_javascript_doc_ready_functions'] = "additional_doc_ready_code();";
      
        $form_basic_info    =   $this->_set_form_basics();
        
        $basic_data['model_lookup'] = set_lookup($this->Lookup_model->Load_lookup_info('Model'),'lookup_id','short_name', BLANK_ROW);
        $basic_data['engine_lookup'] = set_lookup($this->Lookup_model->Load_lookup_info('Engine'),'lookup_id','short_name', BLANK_ROW);
		
		//Set the values for the Member Profile
		$new_car_value['car_id']		=	'';
		$new_car_value['nickname']		=	'';
		$new_car_value['year']			=	'';
		$new_car_value['model_id']		=	'';
		$new_car_value['engine_id']		=	'';
		$new_car_value['owned_from']	=	'';
		$new_car_value['owned_to']		=	'';
		$for_sale_checkbox		= array(
										'name'		=>	'for_sale',
										'id'		=>	'_forSale',
										'value'		=>	'1',
										'checked'	=>	false,
										'class'		=>	'editable',
										);
		$new_car_value['for_sale_checkbox']	=	$for_sale_checkbox;
		$new_car_value['price']				=	'';
		$new_car_value['notes']				=	'';

		
        $data = array_merge($form_basic_info, $basic_data, $new_car_value);
        
        $data['is_data_form']	= true;
        $data['title'] = 'Central Alabama VW Club -- Edit Member Car';
        $data['heading'] = 'Car Info';
        
        $data['view'] = 'car_profile_edit';
        $this->load->vars($data);
        $this->load->view('master_view', $data);
      
	}
	function edit_car() {
        define("CAR_SEGMENT", 3);
		define("EDITABLE", FALSE);
        
        $this->load->model('Lookup_model');
        
        $basic_data['first_name'] = $this->first_name;
        $basic_data['save_type'] = 'update';
	$basic_data['error'] = '';
        $basic_data['additional_javascript_references'] = '<script type="text/javascript" src="'.base_url().'js/member_car_form_functions.js"></script>
                                                           <script type="text/javascript" src="'.base_url().'js/jquery.colorbox-min.js"></script>
                                                          ';
        $basic_data['additional_css_references'] = link_tag('css/colorbox.css'.'?'.time());
        
        
        $car_id = $this->uri->segment(CAR_SEGMENT);
        
        $basic_data['additional_javascript_doc_ready_functions'] = "additional_doc_ready_code();";
      
        $form_basic_info    =   $this->_set_form_basics();
        $car_info           =   $this->_get_car_information($car_id);
        $car_gallery        =   $this->_get_car_photos($car_id, EDITABLE);

        $basic_data['model_lookup'] = set_lookup($this->Lookup_model->Load_lookup_info('Model'),'lookup_id','short_name');
        $basic_data['engine_lookup'] = set_lookup($this->Lookup_model->Load_lookup_info('Engine'),'lookup_id','short_name');
		
        $data = array_merge($form_basic_info, $basic_data, $car_info, $car_gallery);
        
	$data['upload_error']	= $this->session->flashdata('upload_error');
        $data['is_data_form']	= true;
        $data['title'] = 'Central Alabama VW Club -- Edit Member Car';
        $data['heading'] = 'Car Info';
        
        $data['view'] = 'car_profile_edit';
        $this->load->vars($data);        
        $this->load->view('master_view', $data);
        return;
	}
    function do_upload()
	{
        define("ID_SEGMENT", 3);
        $car_id = $this->uri->segment(ID_SEGMENT);
		
        $this->load->helper(array('form', 'url'));
		
		$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/uploads/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '5120'; //5 meg max
		
		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$this->session->set_flashdata('upload_error', $this->upload->display_errors());
			redirect('member_car/edit_car/'.$car_id);
            return;
		}
		
        $upload_data	=	$this->upload->data();
        $new_file_name = $this->process_image($upload_data['file_name'],'member_car_gallery');
        
        if($new_file_name == '') {
            //No file name returned, it's an error. do something
        }
        
        $new_file_name = str_ireplace('.jpg', '', $new_file_name);
        $data = array('car_id' => $car_id, 'pic_name' => $new_file_name, 'is_display_photo' => '0');
        
        $this->load->model('Car_model');
        $this->Car_model->Add_car_image($data);

        $new_file_name = '';
		
		redirect(base_url().'index.php?/member_car/edit_car/'.$car_id);
	}
	function _set_form_basics($must_be_logged_in = true) {
		$ret_val = '';
		$ret_val['logged_in']  = $this->logged_in;
		$ret_val['is_admin']   = $this->is_admin;

		if ($must_be_logged_in && !$this->logged_in) {
				$message    =   'You must be logged in to perform this action.';
				$this->session->set_flashdata('message', $message);
				redirect('login');
		}

		return $ret_val;
	}

	function update() {
		
		$data = $this->_get_form_post_data();
		$this->Car_model->Update_car($data);
		redirect(base_url().'index.php?/member_profile');
	}

	function _get_car_information($car_id) {
		$this->load->model('Car_model');
        
		$car_row = $this->Car_model->Load_car_info($car_id);

		//Set the values for the Member Profile
		$ret_val['car_id']		=	$car_row->car_id;
		$ret_val['nickname']	=	$car_row->nickname;
		$ret_val['year']		=	$car_row->year;
		$ret_val['model_id']	=	$car_row->model_id;
		$ret_val['engine_id']	=	$car_row->engine_id;
		$ret_val['owned_from']	=	$car_row->owned_from;
		$ret_val['owned_to']	=	$car_row->owned_to;
		$for_sale_checkbox		= array(
										'name'		=>	'for_sale',
										'id'		=>	'_forSale',
										'value'		=>	'1',
										'checked'	=>	$car_row->for_sale,
										'class'		=>	'editable',
										);
		$ret_val['for_sale_checkbox']	=	$for_sale_checkbox;
		$ret_val['price']				=	$car_row->price;
		$ret_val['notes']				=	$car_row->notes;
		$ret_val['model']				=	$car_row->model;
		$ret_val['engine']				=	$car_row->engine;
		$ret_val['for_sale_text']		=	$car_row->for_sale_text;
		$ret_val['member_id']			=	$car_row->member_id;
		$ret_val['contact']				=	$car_row->contact;
		

    return $ret_val;
        
	}
	
    function _get_car_photos($car_id, $is_read_only){
        
        $this->load->model('Car_model');
        
		$car_gallery_array = $this->Car_model->Get_car_gallery($car_id);
        $gallery_html   =   '';
        $gallery_html   .=  '<table class="car-gallery-grid">';
		       
		
        $row_pic_count  =   0;
        $image_row      =   '';
        $action_row     =   '';
        $start_count    =   false;
		foreach($car_gallery_array as $row) {
           if ($start_count)
            $row_pic_count++;
           else
            $start_count =   true;
           
           if ($row_pic_count == MAX_ROW_PICS) {
               $gallery_html .= '<tr>'.$image_row.'</tr>';
			   if (!$is_read_only)
				$gallery_html .= '<tr>'.$action_row.'</tr>';
               $action_row = '';
               $image_row = '';
               $row_pic_count = 0;
           }
               
           $thumb_link  = '<img src="./member_car_gallery/thumbnail/'.$row['pic_name'].'_thumb.jpg"/>';
           $image_link  = './member_car_gallery/'.$row['pic_name'].'.jpg';
           $full_link   = '<a class="carGallery"  href="'.$image_link.'" title="">'.$thumb_link.'</a>';
           $image_row   .=   '<td class="image-cell">'.$full_link.'</td>';
		   if (!$is_read_only)
			$action_row  .= '<td class="action-row"> <a href="'.base_url().'index.php?/member_car/delete_image/'.$row['mcg_id'].'">Delete</a>
				                                        <a href="'.base_url().'index.php?/member_car/make_profile_photo/'.$row['mcg_id'].'">Make Display Photo</a></td>';
		}
	   $gallery_html .= '<tr>'.$image_row.'</tr>';
	   if (!$is_read_only)
		$gallery_html .= '<tr>'.$action_row.'</tr>';

        $gallery_html .= '</table>';
		
		$data['car_gallery'] = $gallery_html;
		
		return $data;
        
    }
function _get_form_post_data()
	{
		$this->load->model('Car_model');
        
		$owned_to = $this->input->post('owned_to');
		if (strtotime($owned_to) !== false)	
			$owned_to = $this->make_us_date_mysql_friendly($owned_to);
		else
			$owned_to = null;

		$owned_from = $this->input->post('owned_from');
		if (strtotime($owned_from) !== false)	
			$owned_from = $this->make_us_date_mysql_friendly($owned_from);
		else
			$owned_from = null;
		
		$price = str_replace(',','', str_replace('$','', $this->input->post('price')));
		if (!is_numeric($price) || $price == '0')
			$price = null;
		
		
		$data = array(	'car_id'		=>	$this->input->post('car_id'),
						'member_id'		=>	$this->session->userdata('member_id'),
						'nickname'		=>	$this->input->post('nickname'),
						'year'			=>	$this->input->post('year'),
						'model_id'		=>	$this->input->post('model_id'),			
						'engine_id'		=>	$this->input->post('engine_id'),			
						'owned_from'	=>	$owned_from,
						'owned_to'		=>	$owned_to,
						'for_sale'		=>	$this->input->post('for_sale'),			
						'price'			=>	$price,
						'notes'			=>	$this->input->post('notes')
					);
	
		return $data;
	}

}
/* End of file member_car.php */
/* Location: ./application/controllers/member_car.php */